/**
  ******************************************************************************
  * @file    user_task.c 
  * @author  SD Electronics xxx
  * @version V1.0
  * @date    xx-xx-xx
  * @brief   xxxxxxxxxxxxxxxxxx
  ******************************************************************************
  */
#include <stdint.h>
#include "os_task.h"
#include "os_msg_q.h"
#include "co_printf.h"
#include "user_task.h"
#include "button.h"
#include "sd_common.h"

#if 0
 uint16_t vbat_vol;

/** 
  * @brief  DefineSTENUM
  */ 
uint16_t user_task_id;
os_timer_t PowerOn_Off_timer;			//开关机定时器
bool PowerOn_Off_flag = false;
/**
  * @}
  */

/**
   * @brief  PowerOn_Off_time_fn
   * @param  None
   * @note	xxx
   * @retval None
   */
static void PowerOn_Off_time_fn(void *param) 
{
	static int PowerOn_Off_Count = 0;
	int K_V = 0;
	//获取左右按键的键值
	K_V = pmu_get_gpio_value(GPIO_PORT_A, GPIO_BIT_4);
	co_printf("user: K_V=%d.\r\n", pmu_get_gpio_value(GPIO_PORT_A, GPIO_BIT_4));
    //开机检测电池电压一次
    struct adc_cfg_t cfg;
    uint16_t result, ref_vol;

    memset((void*)&cfg, 0, sizeof(cfg));
    cfg.src = ADC_TRANS_SOURCE_VBAT;
    cfg.ref_sel = ADC_REFERENCE_INTERNAL;
    cfg.int_ref_cfg = ADC_INTERNAL_REF_1_2;
    cfg.clk_sel = ADC_SAMPLE_CLK_24M_DIV13;
    cfg.clk_div = 0x3f;
    adc_init(&cfg);
    adc_enable(NULL, NULL, 0);
    adc_get_result(ADC_TRANS_SOURCE_VBAT, 0, &result);
    ref_vol = adc_get_ref_voltage(ADC_REFERENCE_INTERNAL);
    vbat_vol = (result * 4 * ref_vol) / 1024;
    co_printf("vbat value: %d mv.\r\n", vbat_vol);
    adc_disable();
    
	//判断是否有按键一直按下
	if(K_V == 0)
	{
		PowerOn_Off_Count++;
		//开机
		if(PowerOn_Off_flag == false)
		{
			if(PowerOn_Off_Count >= 2)
			{
                PowerOn_Off_Count = 0;
                PowerOn_Off_flag = true;
				co_printf("user: Power on.\r\n");

				/* DO something */
                /* 关闭定时器 */
				os_timer_stop(&PowerOn_Off_timer);
			}
		}
	}
	else
	{
		//其他的状态就清零
		PowerOn_Off_Count = 0;
		//判断是关机状态下
		if(PowerOn_Off_flag == false)
		{
            co_printf("user: no key press poweroff.\r\n");
            os_timer_stop(&PowerOn_Off_timer);
            system_power_off(true);
		}
	}
}

/**
  * @brief  user_task_func
  * @param  None
  * @note	xxx
  * @retval None
  */
static int user_task_func(os_event_t *param)
{
    switch(param->event_id)
    {
        case USER_EVT_BUTTON:
            {
                struct button_msg_t *button_msg;
                const char *button_type_str[] = {
                                                    "BUTTON_PRESSED",
                                                    "BUTTON_RELEASED",
                                                    "BUTTON_SHORT_PRESSED",
                                                    "BUTTON_MULTI_PRESSED",
                                                    "BUTTON_LONG_PRESSED",
                                                    "BUTTON_LONG_PRESSING",
                                                    "BUTTON_LONG_RELEASED",
                                                    "BUTTON_LONG_LONG_PRESSED",
                                                    "BUTTON_LONG_LONG_RELEASED",
                                                    "BUTTON_COMB_PRESSED",
                                                    "BUTTON_COMB_RELEASED",
                                                    "BUTTON_COMB_SHORT_PRESSED",
                                                    "BUTTON_COMB_LONG_PRESSED",
                                                    "BUTTON_COMB_LONG_PRESSING",
                                                    "BUTTON_COMB_LONG_RELEASED",
                                                    "BUTTON_COMB_LONG_LONG_PRESSED",
                                                    "BUTTON_COMB_LONG_LONG_RELEASED",
                                                };

                button_msg = (struct button_msg_t *)param->param;
                
                co_printf("KEY 0x%08x, TYPE %s.\r\n", button_msg->button_index, button_type_str[button_msg->button_type]);
                /* 关机处理---对应键值---对应按下(长按、短按等) */
                if(button_msg->button_index == 0x00000010 && button_msg->button_type == 0x04)
                {
                    co_printf("usr: button PA4 shutdown.\r\n");
                    system_power_off(true);    
                }
            }
            break;
    }
    return EVT_CONSUMED;
}

/**
  * @brief  user_task_init
  * @param  None
  * @note	xxx
  * @retval None
  */
void user_task_init(void)
{
    //启动OS timer
	os_timer_init(&PowerOn_Off_timer, PowerOn_Off_time_fn, NULL); 
    user_task_id = os_task_create(user_task_func);
    os_timer_start(&PowerOn_Off_timer, 500, true);
}
#endif
